package com.guhappy.util;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.apache.log4j.*;


/**
 * @author  bduan
 */
public class HibernateUtil {
	
	private HibernateUtil() {
	  }

	  private static SessionFactory sessionFactory;
	  
	  private static Logger logger = Logger.getLogger(HibernateUtil.class);

	  // this will be run only once 
	  static {
	    //init SessionFactory
	    Configuration cfg = new Configuration();
	    if(sessionFactory == null) {
	      try {
	    	//cfg.setInterceptor(new HibernateActionInterceptor());
	        cfg.configure();
	        sessionFactory = cfg.buildSessionFactory();
	        logger.info("Session factory configured successfully.");
	      }
	      catch(Exception e) {
	        logger.error("%%%% Error Creating SessionFactory %%%%" , e);
	      }
	    }
	  }

	  /**
	 * @return
	 * @uml.property  name="sessionFactory"
	 */
	public static SessionFactory getSessionFactory() {
	    return sessionFactory;
	  }

	public static void shutdown() {
		// Close caches and connection pools
		getSessionFactory().close();
	}

}